package com.army.stock.web;

import com.army.stock.domain.ArmyCategory;
import com.army.stock.domain.ArmyDepartment;
import com.army.stock.domain.ArmyInstorageOrder;
import com.army.stock.service.ArmyDepartmentService;
import com.army.stock.service.ArmyInstorageOrderService;
import com.javalife.common.domain.page.PaginatedArrayList;
import com.javalife.common.util.date.DateFormatUtils;
import com.javalife.common.web.url.BaseUrl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * Created by zhangyong on 15/3/7.
 * 军队库存入库管理相关
 */
@Controller
public class ArmyInStorageController {

    private static final Logger logger = LoggerFactory.getLogger(ArmyInStorageController.class);

    private static final String INSTORAGE_LIST = "instorage/list";

    private static final String INSTORAGE_NEW = "instorage/new";

    private static final String INSTORAGE_AVAILABLES = "instorage/availables";

    @Autowired
    private ArmyInstorageOrderService armyInstorageOrderService;

    @Autowired
    private ArmyDepartmentService armyDepartmentService;

    @RequestMapping(value = "/instorage/list", method = RequestMethod.GET)
    public String instorageList(HttpServletRequest request, ModelMap modelMap) {
        BaseUrl baseUrl = new BaseUrl(request);
        PaginatedArrayList<ArmyCategory> pageInfo = armyInstorageOrderService.getInstorageOrderPageInfo(
                baseUrl.getCurrentPage(), 20, baseUrl.getQueryMap());
        modelMap.put("page", pageInfo);
        modelMap.put("baseUrl", baseUrl);
        return INSTORAGE_LIST;
    }

    @RequestMapping(value = "/instorage/new", method = RequestMethod.GET)
    public String addInstoragePage(HttpServletRequest request, ModelMap modelMap) {
        List<ArmyDepartment> armyDepartments = armyDepartmentService.getAllArmyDepartments();
        modelMap.put("armyDepartments", armyDepartments);
        ArmyInstorageOrder armyInstorageOrder = new  ArmyInstorageOrder();
        armyInstorageOrder.setInstorageDate(DateFormatUtils.format(new Date(), "yyyy-MM-dd"));
        modelMap.put("armyInstorageOrder" , armyInstorageOrder);
        return INSTORAGE_NEW;
    }

    @RequestMapping(value = "/instorage/add", method = RequestMethod.POST)
    @ResponseBody
    public String addInstorage(HttpServletRequest request, ModelMap modelMap) {
        String addInstorageJson = request.getParameter("addInstorageJson");
        logger.info("addInstorageJson:{}", addInstorageJson);
        return null;
    }

    @RequestMapping(value = "/instorage/availables", method = RequestMethod.GET)
    public String availables(HttpServletRequest request, ModelMap modelMap) {
        String departmentId = request.getParameter("departmentId");
        return INSTORAGE_AVAILABLES;
    }

}
